//
//  ScanerVC.m
//  Photo
//
//  Created by Raoy on 16/9/13.
//  Copyright © 2016年 Raoy. All rights reserved.
//

#import "ScanerVC.h"
#import "ScanerPhoto.h"

@import AVFoundation;

@interface ScanerVC ()<AVCaptureMetadataOutputObjectsDelegate, UIAlertViewDelegate>

//加载中视图
@property (weak, nonatomic)IBOutlet UIView *loadingView;

//扫码区域动画视图
@property (weak, nonatomic)IBOutlet ScanerPhoto *scanView;

//AV 协调器
@property(strong, nonatomic)AVCaptureSession  *session;
//取景视图
@property(strong, nonatomic)AVCaptureVideoPreviewLayer *previewLayer;

@end

@implementation ScanerVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.scanView.alpha = 0;
    //设置扫描区域边长
    self.scanView.scanAreaEdgeLength = DeviceWidth - 2 * 50;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (!self.session) {
        //添加镜头开启动画
        CATransition *animated = [CATransition animation];
        animated.duration = 0.5;
        animated.type = @"cameraIrisHollowOpen";
        animated.timingFunction = UIViewAnimationOptionCurveEaseInOut;
        animated.delegate = self;
        [self.view.layer addAnimation:animated forKey:@"animation"];
        
        //初始化扫描
        [self setScaner];
        
        //调整摄像头取景区域
        CGRect rect = self.view.bounds;
        rect.origin.y = self.navigationController.navigationBarHidden ? 0 : 64;
        self.previewLayer.frame = rect;
    }
}

// 动画结束回调
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    self.loadingView.hidden = YES;
    [UIView animateWithDuration:0.25
                     animations:^{
                         self.scanView.alpha = 1;
                     }];
}

//初始化扫描
- (void)setScaner
{
    //创建会话
    self.session = [[AVCaptureSession alloc] init];
    
    //获取摄像头设备
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;
    
    //创建输入流
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    
    if (input) {
        [self.session addInput:input];
    }else{
    
        //出错处理
        NSLog(@"%@", error);
        NSString *msg = [NSString stringWithFormat:@"请在手机【设置】-【隐私】-【相机】选项中，允许【%@】访问您的相机", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                    message:msg
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles: nil];
        [alert show];
        return;
    }
    
    //创建输出流
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    [self.session addOutput:output];
    
    //设置扫码类型
    output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode,
                                   AVMetadataObjectTypeEAN13Code, //条形码
                                   AVMetadataObjectTypeEAN8Code,
                                   AVMetadataObjectTypeCode128Code
                                   ];
    //设置代理，在主线程刷新
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    //创建摄像头取景区域
    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer insertSublayer:self.previewLayer atIndex:0];
    
    if ([self.previewLayer connection].isVideoMirroringSupported) {
        [self.previewLayer connection].videoOrientation = AVCaptureVideoOrientationPortrait;
    }
    
    __weak typeof(self) weakSelf = self;
    [[NSNotificationCenter defaultCenter] addObserverForName:AVCaptureInputPortFormatDescriptionDidChangeNotification
                                                      object:nil
                                                       queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
                                                           if (weakSelf) {
                                                               //调整扫码区域
                                                               AVCaptureMetadataOutput *output = weakSelf.session.outputs.firstObject;
                                                               output.rectOfInterest = [weakSelf.previewLayer metadataOutputRectOfInterestForRect:weakSelf.scanView.scanAreaRect];
                                                               
                                                           }
                                                       }];
    //开始扫码
    [self.session startRunning];
    
}

#pragma mark - AVCaptureMetadataOutputObjects Delegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    for (AVMetadataMachineReadableCodeObject *metadata in metadataObjects) {
        if ([metadata.type  isEqualToString:AVMetadataObjectTypeQRCode]) {
            [self.session stopRunning];
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"二维码"
//                                                            message:metadata.stringValue delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
//            [alert show];
            [self requireDelegate:metadata.stringValue];
            break;
        } else {
            [self.session stopRunning];
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"条形码"
//                                                            message:metadata.stringValue delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
//            [alert show];
            [self requireDelegate:metadata.stringValue];
            break;
        }
        
        
    }
}

- (void)requireDelegate:(NSString *)metaString
{
    if ([self.qrCodedelegate respondsToSelector:@selector(qrCodeValueDelegate:)]) {
        [self.qrCodedelegate qrCodeValueDelegate:metaString];
    }
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - UIAlertView Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
